<template>
    <!-- 自定义的时间控件 -->
    <div class="custom-time flex-wrap flex-text-mid" v-if="options > 1">
        <template v-for="(item, index) in options"  :key="index">
            <span
            class="time-text"
            v-if="index !== 0">-</span>
            <button
            plain
            @click="toggleTime"
            :style="{
                width: options > 1 ? 'calc((100% - ' + 35*(options - 1) + 'px)/' + options + ')' : '100%'
            }">{{ checked[index] || '请选择' }}</button>
        </template>
        <van-calendar ref="calendar" v-model:value="timeShow" type="range" @confirm="onConfirm"/>
    </div>
    <div class="custom-time flex-wrap flex-text-mid" v-else>
        <button
        plain
        @click="toggleTime"
        :style="{
            width: options > 1 ? 'calc((100% - ' + 35*(options - 1) + 'px)/' + options + ')' : '100%'
        }">{{ checked[0] || '请选择' }}</button>
        <van-calendar ref="calendar" v-model:value="timeShow" @confirm="onConfirm"/>
    </div>
</template>

<script>
import { parse, stringify } from '@/assets/js/util'
	import vanCalendar from '@/components/vant/van-calendar/van-calendar.vue'
export default {
	components:{
		vanCalendar
	},
    model: {
        prop: 'checked',
        event: 'change'
    },
    data(){
        return {
            timeShow: false,
        }
    },
    props: {
        checked: {
            type: Array,
            default: () => []
        },
        options: {
            type: Number,
            default: 1
        },
    },
    methods: {
        toggleTime(index) {
			console.log("index",index)
            this.timeShow = true
        },
        formatDate(date) {
            return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`
        },
        onConfirm (date) {
            this.timeShow = false
			let time = date.range?.before || date.range?.after
			let oldDate = time ? [time, date.range.after || time] : []
			if (!oldDate.length && date.fulldate) {
				oldDate = [date.fulldate, date.fulldate]
			}
			let arr;
            if (this.options > 1) {
                const [start, end] = oldDate
                arr = [start, end]
            } else {
                arr = [oldDate]
            }
			this.$emit('update:checked', arr);
            this.$emit('change', arr);
        },
        reset() {
			this.$emit('update:checked', []);
            // this.$refs.calendar && this.$refs.calendar.reset()
        }
    }
}
</script>

<style lang="scss" scoped>
    @import '@/static/css/variable.scss';
    .custom-time {
        margin-bottom: 9px;
        button {
            height: 48px;
            width: 77px;
            color: #BBBBBB;
            padding: 0;
            background: #F6F6F6;
            border-radius: 4px;
            border: 1px solid #EEEEEE;
            &:before {
                display: none;
            }
        }
        .time-text{
            width: 35PX;
            text-align: center;
            font-size: 14px;
            font-weight: 400;
            color: #666666;
            line-height: 20px;
        }
        .van-button--primary {
            color: $primaryColor;
            background: #fff;
            border-color: $primaryColor;
        }
    }
</style>